package netty.plc;

import com.hslCommunication.core.net.networkBase.NetworkDeviceBase;
import com.plc.sdk.hslCommunication.StringResources;
import com.plc.sdk.hslCommunication.core.iMessage.S7Message;
import com.plc.sdk.hslCommunication.core.transfer.ReverseBytesTransform;
import com.plc.sdk.hslCommunication.core.types.OperateResult;
import com.plc.sdk.hslCommunication.core.types.OperateResultExOne;
import com.plc.sdk.hslCommunication.core.types.OperateResultExThree;
import com.plc.sdk.siemens.SiemensPLCS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.net.Socket;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author zhangtao
 * @create 2021/11/23 10:48
 */
public class SiemensS7Net extends NetworkDeviceBase<S7Message, ReverseBytesTransform> {
    private byte[] plcHead1 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -64, 1, 10, -63, 2, 1, 2, -62, 2, 1, 0};
    private byte[] plcHead2 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 4, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 1, -32};
    private byte[] plcOrderNumber = new byte[]{3, 0, 0, 33, 2, -16, -128, 50, 7, 0, 0, 0, 1, 0, 8, 0, 8, 0, 1, 18, 4, 17, 68, 1, 0, -1, 9, 0, 4, 0, 17, 0, 0};
    private SiemensPLCS CurrentPlc;
    private byte[] plcHead1_200smart;
    private byte[] plcHead2_200smart;
    private byte[] plcHead1_200;
    private byte[] plcHead2_200;
    private byte plc_slot;
    private byte plc_rack;
    ThreadLocalRandom random = ThreadLocalRandom.current();
    private static final Logger log = LoggerFactory.getLogger(SiemensS7Net.class);

    public SiemensS7Net(SiemensPLCS siemens) {
        super(S7Message.class, ReverseBytesTransform.class);
        this.CurrentPlc = SiemensPLCS.S1200;
        this.plcHead1_200smart = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 16, 0, -62, 2, 3, 0, -64, 1, 10};
        this.plcHead2_200smart = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, -52, -63, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
        this.plcHead1_200 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 77, 87, -62, 2, 77, 87, -64, 1, 9};
        this.plcHead2_200 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 0, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
        this.plc_slot = 0;
        this.plc_rack = 0;
        this.Initialization(siemens, "");
    }

    public SiemensS7Net(SiemensPLCS siemens, String ipAddress) {
        super(S7Message.class, ReverseBytesTransform.class);
        this.CurrentPlc = SiemensPLCS.S1200;
        this.plcHead1_200smart = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 16, 0, -62, 2, 3, 0, -64, 1, 10};
        this.plcHead2_200smart = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, -52, -63, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
        this.plcHead1_200 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 77, 87, -62, 2, 77, 87, -64, 1, 9};
        this.plcHead2_200 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 0, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
        this.plc_slot = 0;
        this.plc_rack = 0;
        this.Initialization(siemens, ipAddress);
    }

    private void Initialization(SiemensPLCS siemens, String ipAddress) {
        this.WordLength = 2;
        this.setIpAddress(ipAddress);
        this.setPort(102);
        this.CurrentPlc = siemens;
        switch (siemens.ordinal()) {
            case 1:
                this.plcHead1[21] = 0;
                break;
            case 2:
                this.plcHead1[21] = 2;
                break;
            case 3:
                this.plcHead1[21] = 3;
                this.plcHead1[17] = 0;
                break;
            case 4:
                this.plcHead1[21] = 0;
                break;
            case 5:
                this.plcHead1 = this.plcHead1_200smart;
                this.plcHead2 = this.plcHead2_200smart;
                break;
            case 6:
                this.plcHead1 = this.plcHead1_200;
                this.plcHead2 = this.plcHead2_200;
            default:
                this.plcHead1[18] = 0;
        }

    }

    public byte getSlot() {
        return this.plc_slot;
    }

    public void setSlot(byte value) {
        this.plc_slot = value;
        this.plcHead1[21] = (byte) (this.plc_rack * 32 + this.plc_slot);
    }

    public byte getRack() {
        return this.plc_rack;
    }

    public void setRack(byte value) {
        this.plc_rack = value;
        this.plcHead1[21] = (byte) (this.plc_rack * 32 + this.plc_slot);
    }

    @Override
    protected OperateResult InitializationOnConnect(Socket socket) {
        return OperateResult.CreateSuccessResult();
    }

    public OperateResultExOne<String> ReadOrderNumber() {
        OperateResultExOne<String> result = new OperateResultExOne();
        result.IsSuccess = true;
        result.Content = "1";
        return result;
    }

    @Override
    public OperateResultExOne<byte[]> Read(String address, short length) {
        OperateResultExThree<Byte, Integer, Integer> addressResult = AnalysisAddress(address);
        if (!addressResult.IsSuccess) {
            return OperateResultExOne.CreateFailedResult(addressResult);
        } else {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            short readLength;
            for (short alreadyFinished = 0; alreadyFinished < length; addressResult.Content2 = (Integer) addressResult.Content2 + readLength * 8) {
                readLength = (short) Math.min(length - alreadyFinished, 200);
                OperateResultExThree[] list = new OperateResultExThree[]{addressResult};
                OperateResultExOne<byte[]> read = this.Read(list, new short[]{readLength});
                if (!read.IsSuccess) {
                    return read;
                }

                try {
                    outputStream.write((byte[]) read.Content);
                } catch (Exception var11) {
                }

                alreadyFinished += readLength;
            }

            byte[] buffer = outputStream.toByteArray();

            try {
                outputStream.close();
            } catch (Exception var10) {
            }

            return OperateResultExOne.CreateSuccessResult(buffer);
        }
    }

    private OperateResultExOne<byte[]> ReadBitFromPLC(String address) {
        OperateResultExOne<byte[]> command = BuildBitReadCommand(address);
        if (!command.IsSuccess) {
            return command;
        } else {
            OperateResultExOne<byte[]> read = this.ReadFromCoreServer((byte[]) command.Content);
            if (!read.IsSuccess) {
                return read;
            } else {
                int receiveCount = 1;
                if (((byte[]) read.Content).length >= 21 && ((byte[]) read.Content)[20] == 1) {
                    byte[] buffer = new byte[receiveCount];
                    if (22 < ((byte[]) read.Content).length && ((byte[]) read.Content)[21] == -1 && ((byte[]) read.Content)[22] == 3) {
                        buffer[0] = ((byte[]) read.Content)[25];
                    }

                    return OperateResultExOne.CreateSuccessResult(buffer);
                } else {
                    return new OperateResultExOne(read.ErrorCode, StringResources.Language.SiemensDataLengthCheckFailed());
                }
            }
        }
    }

    public OperateResultExOne<byte[]> Read(String[] address, short[] length) {
        OperateResultExThree[] list = new OperateResultExThree[address.length];

        for (int i = 0; i < address.length; ++i) {
            OperateResultExThree<Byte, Integer, Integer> tmp = AnalysisAddress(address[i]);
            if (!tmp.IsSuccess) {
                return OperateResultExOne.CreateFailedResult(tmp);
            }

            list[i] = tmp;
        }

        return this.Read(list, length);
    }

    private OperateResultExOne<byte[]> Read(OperateResultExThree<Byte, Integer, Integer>[] address, short[] length) {
        byte[] b = new byte[length.length];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) random.nextInt(1, 100);
        }
        OperateResultExOne<byte[]> result = OperateResultExOne.CreateSuccessResult(b);
        return result;
    }

    public OperateResultExOne<Boolean> ReadBool(String address) {
        OperateResultExOne<Boolean> result = new OperateResultExOne();
        result.IsSuccess = true;
        result.Content = true;
        return result;
    }

    public OperateResultExOne<Byte> ReadByte(String address) {
        OperateResultExOne<Byte> result = new OperateResultExOne();
        result.IsSuccess = true;
        result.Content = (byte) random.nextInt(1, 24);
        log.info("plc ReadByte：{}", result.Content);
        return result;
    }

    @Override
    public OperateResultExOne<Integer> ReadInt32(String address) {
        OperateResultExOne<Integer> result = new OperateResultExOne();
        result.IsSuccess = true;
        result.Content = random.nextInt(1, 24);
        log.info("plc ReadInt32：{}", result.Content);
        return result;
    }

    @Override
    public OperateResultExOne<Short> ReadInt16(String address) {
        OperateResultExOne<Short> result = new OperateResultExOne();
        result.IsSuccess = true;
        result.Content = (short) random.nextInt(1, 24);
        log.info("plc ReadInt16：{}", result.Content);
        return result;
    }

    @Override
    public OperateResultExOne<Float> ReadFloat(String address) {
        OperateResultExOne<Float> result = new OperateResultExOne();
        result.IsSuccess = true;
        result.Content = random.nextInt(1, 300) / 10.F;
        log.info("plc ReadFloat：{}", result.Content);
        return result;
    }

    @Override
    public OperateResultExOne<Double> ReadDouble(String address) {
        OperateResultExOne<Double> result = new OperateResultExOne();
        result.IsSuccess = true;
        result.Content = random.nextInt(1, 300) / 10.0;
        log.info("plc ReadDouble：{}", result.Content);
        return result;
    }

    private OperateResult WriteBase(byte[] entireValue) {
        return OperateResult.CreateSuccessResult();
    }

    @Override
    public OperateResult Write(String address, byte[] value) {
        return OperateResult.CreateSuccessResult();
    }

    public OperateResult Write(String address, boolean value) {
        return OperateResult.CreateSuccessResult();
    }

    public OperateResult Write(String address, boolean[] values) {
        return OperateResult.CreateSuccessResult();
    }

    public OperateResult Write(String address, byte value) {
        return OperateResult.CreateSuccessResult();
    }

    public OperateResultExOne<String> ReadString(String address) {
        OperateResultExOne read;
        OperateResultExOne readString;
        byte[] buffer;
        if (this.CurrentPlc != SiemensPLCS.S200Smart) {
            read = this.Read(address, (short) 2);
            if (!read.IsSuccess) {
                return OperateResultExOne.CreateFailedResult(read);
            } else if (((byte[]) read.Content)[0] != -2) {
                return new OperateResultExOne("Value in plc is not string type");
            } else {
                readString = this.Read(address, (short) (2 + ((byte[]) read.Content)[1]));
                if (!readString.IsSuccess) {
                    return OperateResultExOne.CreateFailedResult(readString);
                } else {
                    try {
                        buffer = new byte[((byte[]) read.Content)[1]];
                        System.arraycopy(readString.Content, 2, buffer, 0, buffer.length);
                        return OperateResultExOne.CreateSuccessResult(new String(buffer, "US-ASCII"));
                    } catch (Exception var5) {
                        return new OperateResultExOne(var5.getMessage());
                    }
                }
            }
        } else {
            read = this.Read(address, (short) 1);
            if (!read.IsSuccess) {
                return OperateResultExOne.CreateFailedResult(read);
            } else {
                readString = this.Read(address, (short) (1 + ((byte[]) read.Content)[0]));
                if (!readString.IsSuccess) {
                    return OperateResultExOne.CreateFailedResult(readString);
                } else {
                    try {
                        buffer = new byte[((byte[]) read.Content)[0]];
                        System.arraycopy(readString.Content, 1, buffer, 0, buffer.length);
                        return OperateResultExOne.CreateSuccessResult(new String(buffer, "US-ASCII"));
                    } catch (Exception var6) {
                        return new OperateResultExOne(var6.getMessage());
                    }
                }
            }
        }
    }

    @Override
    public OperateResult Write(String address, String value) {
        return OperateResult.CreateSuccessResult();
    }

    @Override
    public String toString() {
        return "SiemensS7Net";
    }

    public static int CalculateAddressStarted(String address) {
        if (address.indexOf(46) < 0) {
            return Integer.parseInt(address) * 8;
        } else {
            String[] temp = address.split("\\.");
            return Integer.parseInt(temp[0]) * 8 + Integer.parseInt(temp[1]);
        }
    }

    public static OperateResultExThree<Byte, Integer, Integer> AnalysisAddress(String address) {
        OperateResultExThree result = new OperateResultExThree();

        try {
            result.Content3 = 0;
            if (address.charAt(0) == 'I') {
                result.Content1 = -127;
                result.Content2 = CalculateAddressStarted(address.substring(1));
            } else if (address.charAt(0) == 'Q') {
                result.Content1 = -126;
                result.Content2 = CalculateAddressStarted(address.substring(1));
            } else if (address.charAt(0) == 'M') {
                result.Content1 = -125;
                result.Content2 = CalculateAddressStarted(address.substring(1));
            } else if (address.charAt(0) != 'D' && address.substring(0, 2) != "DB") {
                if (address.charAt(0) == 'T') {
                    result.Content1 = 29;
                    result.Content2 = CalculateAddressStarted(address.substring(1));
                } else if (address.charAt(0) == 'C') {
                    result.Content1 = 28;
                    result.Content2 = CalculateAddressStarted(address.substring(1));
                } else {
                    if (address.charAt(0) != 'V') {
                        return new OperateResultExThree(StringResources.Language.NotSupportedDataType());
                    }

                    result.Content1 = -124;
                    result.Content3 = 1;
                    result.Content2 = CalculateAddressStarted(address.substring(1));
                }
            } else {
                result.Content1 = -124;
                String[] adds = address.split("\\.");
                if (address.charAt(1) == 'B') {
                    result.Content3 = Integer.parseInt(adds[0].substring(2));
                } else {
                    result.Content3 = Integer.parseInt(adds[0].substring(1));
                }

                result.Content2 = CalculateAddressStarted(address.substring(address.indexOf(46) + 1));
            }
        } catch (Exception var3) {
            result.Message = var3.getMessage();
            return result;
        }

        result.IsSuccess = true;
        return result;
    }

    public static OperateResultExOne<byte[]> BuildReadCommand(String address, short length) {
        OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
        if (!analysis.IsSuccess) {
            return OperateResultExOne.CreateFailedResult(analysis);
        } else {
            OperateResultExThree[] addressList = new OperateResultExThree[]{analysis};
            short[] lengthList = new short[]{length};
            return BuildReadCommand(addressList, lengthList);
        }
    }

    public static OperateResultExOne<byte[]> BuildReadCommand(OperateResultExThree<Byte, Integer, Integer>[] address, short[] length) {
        if (address == null) {
            throw new RuntimeException("address is null");
        } else if (length == null) {
            throw new RuntimeException("count is null");
        } else if (address.length != length.length) {
            throw new RuntimeException(StringResources.Language.TwoParametersLengthIsNotSame());
        } else if (length.length > 19) {
            throw new RuntimeException(StringResources.Language.SiemensReadLengthCannotLargerThan19());
        } else {
            int readCount = length.length;
            byte[] _PLCCommand = new byte[19 + readCount * 12];
            _PLCCommand[0] = 3;
            _PLCCommand[1] = 0;
            _PLCCommand[2] = (byte) (_PLCCommand.length / 256);
            _PLCCommand[3] = (byte) (_PLCCommand.length % 256);
            _PLCCommand[4] = 2;
            _PLCCommand[5] = -16;
            _PLCCommand[6] = -128;
            _PLCCommand[7] = 50;
            _PLCCommand[8] = 1;
            _PLCCommand[9] = 0;
            _PLCCommand[10] = 0;
            _PLCCommand[11] = 0;
            _PLCCommand[12] = 1;
            _PLCCommand[13] = (byte) ((_PLCCommand.length - 17) / 256);
            _PLCCommand[14] = (byte) ((_PLCCommand.length - 17) % 256);
            _PLCCommand[15] = 0;
            _PLCCommand[16] = 0;
            _PLCCommand[17] = 4;
            _PLCCommand[18] = (byte) readCount;

            for (int ii = 0; ii < readCount; ++ii) {
                _PLCCommand[19 + ii * 12] = 18;
                _PLCCommand[20 + ii * 12] = 10;
                _PLCCommand[21 + ii * 12] = 16;
                _PLCCommand[22 + ii * 12] = 2;
                _PLCCommand[23 + ii * 12] = (byte) (length[ii] / 256);
                _PLCCommand[24 + ii * 12] = (byte) (length[ii] % 256);
                _PLCCommand[25 + ii * 12] = (byte) ((Integer) address[ii].Content3 / 256);
                _PLCCommand[26 + ii * 12] = (byte) ((Integer) address[ii].Content3 % 256);
                _PLCCommand[27 + ii * 12] = (Byte) address[ii].Content1;
                _PLCCommand[28 + ii * 12] = (byte) ((Integer) address[ii].Content2 / 256 / 256 % 256);
                _PLCCommand[29 + ii * 12] = (byte) ((Integer) address[ii].Content2 / 256 % 256);
                _PLCCommand[30 + ii * 12] = (byte) ((Integer) address[ii].Content2 % 256);
            }

            return OperateResultExOne.CreateSuccessResult(_PLCCommand);
        }
    }

    public static OperateResultExOne<byte[]> BuildBitReadCommand(String address) {
        byte[] _PLCCommand = new byte[31];
        _PLCCommand[0] = 3;
        _PLCCommand[1] = 0;
        _PLCCommand[2] = (byte) (_PLCCommand.length / 256);
        _PLCCommand[3] = (byte) (_PLCCommand.length % 256);
        _PLCCommand[4] = 2;
        _PLCCommand[5] = -16;
        _PLCCommand[6] = -128;
        _PLCCommand[7] = 50;
        _PLCCommand[8] = 1;
        _PLCCommand[9] = 0;
        _PLCCommand[10] = 0;
        _PLCCommand[11] = 0;
        _PLCCommand[12] = 1;
        _PLCCommand[13] = (byte) ((_PLCCommand.length - 17) / 256);
        _PLCCommand[14] = (byte) ((_PLCCommand.length - 17) % 256);
        _PLCCommand[15] = 0;
        _PLCCommand[16] = 0;
        _PLCCommand[17] = 4;
        _PLCCommand[18] = 1;
        OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
        if (!analysis.IsSuccess) {
            return OperateResultExOne.CreateFailedResult(analysis);
        } else {
            _PLCCommand[19] = 18;
            _PLCCommand[20] = 10;
            _PLCCommand[21] = 16;
            _PLCCommand[22] = 1;
            _PLCCommand[23] = 0;
            _PLCCommand[24] = 1;
            _PLCCommand[25] = (byte) ((Integer) analysis.Content3 / 256);
            _PLCCommand[26] = (byte) ((Integer) analysis.Content3 % 256);
            _PLCCommand[27] = (Byte) analysis.Content1;
            _PLCCommand[28] = (byte) ((Integer) analysis.Content2 / 256 / 256 % 256);
            _PLCCommand[29] = (byte) ((Integer) analysis.Content2 / 256 % 256);
            _PLCCommand[30] = (byte) ((Integer) analysis.Content2 % 256);
            return OperateResultExOne.CreateSuccessResult(_PLCCommand);
        }
    }

    public static OperateResultExOne<byte[]> BuildWriteByteCommand(String address, byte[] data) {
        if (data == null) {
            data = new byte[0];
        }

        OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
        return !analysis.IsSuccess ? OperateResultExOne.CreateFailedResult(analysis) : BuildWriteByteCommand(analysis, data);
    }

    public static OperateResultExOne<byte[]> BuildWriteByteCommand(OperateResultExThree<Byte, Integer, Integer> analysis, byte[] data) {
        if (data == null) {
            data = new byte[0];
        }

        byte[] _PLCCommand = new byte[35 + data.length];
        _PLCCommand[0] = 3;
        _PLCCommand[1] = 0;
        _PLCCommand[2] = (byte) ((35 + data.length) / 256);
        _PLCCommand[3] = (byte) ((35 + data.length) % 256);
        _PLCCommand[4] = 2;
        _PLCCommand[5] = -16;
        _PLCCommand[6] = -128;
        _PLCCommand[7] = 50;
        _PLCCommand[8] = 1;
        _PLCCommand[9] = 0;
        _PLCCommand[10] = 0;
        _PLCCommand[11] = 0;
        _PLCCommand[12] = 1;
        _PLCCommand[13] = 0;
        _PLCCommand[14] = 14;
        _PLCCommand[15] = (byte) ((4 + data.length) / 256);
        _PLCCommand[16] = (byte) ((4 + data.length) % 256);
        _PLCCommand[17] = 5;
        _PLCCommand[18] = 1;
        _PLCCommand[19] = 18;
        _PLCCommand[20] = 10;
        _PLCCommand[21] = 16;
        _PLCCommand[22] = 2;
        _PLCCommand[23] = (byte) (data.length / 256);
        _PLCCommand[24] = (byte) (data.length % 256);
        _PLCCommand[25] = (byte) ((Integer) analysis.Content3 / 256);
        _PLCCommand[26] = (byte) ((Integer) analysis.Content3 % 256);
        _PLCCommand[27] = (Byte) analysis.Content1;
        _PLCCommand[28] = (byte) ((Integer) analysis.Content2 / 256 / 256 % 256);
        _PLCCommand[29] = (byte) ((Integer) analysis.Content2 / 256 % 256);
        _PLCCommand[30] = (byte) ((Integer) analysis.Content2 % 256);
        _PLCCommand[31] = 0;
        _PLCCommand[32] = 4;
        _PLCCommand[33] = (byte) (data.length * 8 / 256);
        _PLCCommand[34] = (byte) (data.length * 8 % 256);
        System.arraycopy(data, 0, _PLCCommand, 35, data.length);
        return OperateResultExOne.CreateSuccessResult(_PLCCommand);
    }

    public static OperateResultExOne<byte[]> BuildWriteBitCommand(String address, boolean data) {
        OperateResultExThree<Byte, Integer, Integer> analysis = AnalysisAddress(address);
        if (!analysis.IsSuccess) {
            return OperateResultExOne.CreateFailedResult(analysis);
        } else {
            byte[] buffer = new byte[]{(byte) (data ? 1 : 0)};
            byte[] _PLCCommand = new byte[35 + buffer.length];
            _PLCCommand[0] = 3;
            _PLCCommand[1] = 0;
            _PLCCommand[2] = (byte) ((35 + buffer.length) / 256);
            _PLCCommand[3] = (byte) ((35 + buffer.length) % 256);
            _PLCCommand[4] = 2;
            _PLCCommand[5] = -16;
            _PLCCommand[6] = -128;
            _PLCCommand[7] = 50;
            _PLCCommand[8] = 1;
            _PLCCommand[9] = 0;
            _PLCCommand[10] = 0;
            _PLCCommand[11] = 0;
            _PLCCommand[12] = 1;
            _PLCCommand[13] = 0;
            _PLCCommand[14] = 14;
            _PLCCommand[15] = (byte) ((4 + buffer.length) / 256);
            _PLCCommand[16] = (byte) ((4 + buffer.length) % 256);
            _PLCCommand[17] = 5;
            _PLCCommand[18] = 1;
            _PLCCommand[19] = 18;
            _PLCCommand[20] = 10;
            _PLCCommand[21] = 16;
            _PLCCommand[22] = 1;
            _PLCCommand[23] = (byte) (buffer.length / 256);
            _PLCCommand[24] = (byte) (buffer.length % 256);
            _PLCCommand[25] = (byte) ((Integer) analysis.Content3 / 256);
            _PLCCommand[26] = (byte) ((Integer) analysis.Content3 % 256);
            _PLCCommand[27] = (Byte) analysis.Content1;
            _PLCCommand[28] = (byte) ((Integer) analysis.Content2 / 256 / 256);
            _PLCCommand[29] = (byte) ((Integer) analysis.Content2 / 256);
            _PLCCommand[30] = (byte) ((Integer) analysis.Content2 % 256);
            _PLCCommand[31] = 0;
            _PLCCommand[32] = 3;
            _PLCCommand[33] = (byte) (buffer.length / 256);
            _PLCCommand[34] = (byte) (buffer.length % 256);
            System.arraycopy(buffer, 0, _PLCCommand, 35, buffer.length);
            return OperateResultExOne.CreateSuccessResult(_PLCCommand);
        }
    }
}
